package fhv.itb.ec.soap;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.MimetypesFileTypeMap;


public class SOAPClient {

    private File m_root;
    private File m_file;
    private SOAPServer m_stub;
    BufferedReader m_in;
    MimetypesFileTypeMap m_types;


    public SOAPClient(SOAPServer stub) {
        m_root = new File("/Users/Malcolm/School/SEM5/Enterprise_Computing/" +
                "Assignments/Ex8/tmpclt");
        m_file = m_root;
        m_stub = stub;
        m_in = new BufferedReader(new InputStreamReader(System.in));
        m_types = new MimetypesFileTypeMap();


        getInput();
    }


    public static void main(String[] args) {
        SOAPServerService factory = new SOAPServerService();
        SOAPServer stub = factory.getSOAPServerPort();
        SOAPClient sc = new SOAPClient(stub);
        System.out.println(stub.getDir());
    }


    private void getInput() {

        if (m_in != null) {
            System.out.print("\n>");

            try {
                String input = m_in.readLine();

                while (input != null && !input.equals("quit")) {

                    if (input.equals("getdir")) {
                        System.out.print("current directory is " + m_stub.getDir() + "\n\n>");
                    } else if (input.startsWith("cd")) {

                        if (m_stub.changeDir(input.substring(3)) == true) {
                            System.out.print("cd succeeded\n\n>");
                        } else {
                            System.out.print("cd failed\n\n>");
                        }

                    } else if (input.equals("ls")) {
                        System.out.println(m_stub.list());
                        System.out.print(">");
                    } else if (input.startsWith("get ")) {
                        File f = new File(m_file.getPath() + File.separator + input.substring(4));

                        if (!f.exists()) {
                            f.createNewFile();
                        }

                        FileOutputStream os = new FileOutputStream(f);

                        if (os != null) {
                            os.write(m_stub.read(input.substring(4)));
                            System.out.print("get succeeded - got file " +
                                    f.getName() + "\n\n>");
                            os.close();
                        } else {
                            System.out.print("Could not create FileOutputStream\n\n>");
                        }

                    } else if (input.startsWith("put ")) {
                        File f = new File(m_file.getPath() + File.separator + input.substring(4));
                        System.out.println("Trying to upload file " + f.getPath());

                        if (f.exists()) {
                            FileInputStream is = new FileInputStream(f);
                            Vector<Byte> bytes = new Vector<Byte>();
                            int b;

                            while ((b = is.read()) != -1) {
                                bytes.add(new Byte((byte) b));
                            }

                            byte[] outData = new byte[bytes.size()];

                            for (int i = 0; i < bytes.size(); i++) {
                                outData[i] = bytes.elementAt(i);
                            }

                            if (m_stub.write(outData, input.substring(4)) == true) {
                                System.out.print("put succeeded - uploaded " +
                                        outData.length + " bytes\n\n>");
                            } else {
                                System.out.print("put failed\n\n>");
                            }

                        } else {
                            System.out.print("put failed - file not found\n\n>");
                        }

                    } else {
                        System.out.println("Unsupported command\n\n>");
                    }

                    input = m_in.readLine();
                }

            } catch (IOException ex) {
                Logger.getLogger(SOAPClient.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }
}
